package com.kingdee.eas.jky.pm.app;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.ui.face.UIRuleUtil;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.DefineProjectFactory;
import com.kingdee.eas.custom.DefineProjectInfo;
import com.kingdee.eas.jky.pm.CostAffirmInfo;
import com.kingdee.eas.jky.pm.CostBalanceFactory;
import com.kingdee.eas.jky.pm.CostBalanceInfo;
import com.kingdee.eas.jky.pm.EstimatedFinBillFactory;
import com.kingdee.eas.jky.pm.EstimatedFinBillInfo;
import com.kingdee.eas.util.app.DbUtil;

public class EstimatedFinBillControllerBean extends AbstractEstimatedFinBillControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.jky.pm.app.EstimatedFinBillControllerBean");
    protected void _audit(Context ctx, IObjectPK pk)throws BOSException, EASBizException
    {
         super._audit(ctx, pk);
         EstimatedFinBillInfo easInfo = (EstimatedFinBillInfo) this.getValue(ctx, pk);
         if(easInfo.getProjectNumber()!=null){
        	 String projectID=easInfo.getProjectNumber().getId().toString();
        	 DefineProjectInfo projectInfo= DefineProjectFactory.getLocalInstance(ctx).getDefineProjectInfo(new ObjectUuidPK(projectID));
        	 projectInfo.setYfzgAmount(UIRuleUtil.getBigDecimal(projectInfo.getYfzgAmount()).add(UIRuleUtil.getBigDecimal(easInfo.getEstimateAmount())));
        	 projectInfo.setNotkpzgAmount(UIRuleUtil.getBigDecimal(projectInfo.getNotkpzgAmount()).add(UIRuleUtil.getBigDecimal(easInfo.getEstimateAmount())));
        	 DefineProjectFactory.getLocalInstance(ctx).update(new ObjectUuidPK(projectID), projectInfo);
         }
//         if(easInfo.getSourceBillId()!=null){
//        	 CostBalanceInfo costBanInfo= CostBalanceFactory.getLocalInstance(ctx).getCostBalanceInfo(new ObjectUuidPK(easInfo.getSourceBillId())) ;
//             costBanInfo.setYfzgFinish(true);
//             costBanInfo.setZgyuAmount(costBanInfo.getZgyuAmount().subtract(easInfo.getEstimateAmount()));
//             CostBalanceFactory.getLocalInstance(ctx).update(new ObjectUuidPK(easInfo.getSourceBillId()), costBanInfo);
//         }
    }
    protected void _unAudit(Context ctx, IObjectPK pk)throws BOSException, EASBizException
    {
         super._unAudit(ctx, pk);
         EstimatedFinBillInfo easInfo = (EstimatedFinBillInfo) this.getValue(ctx, pk);
         if(easInfo.getProjectNumber()!=null){
        	 String projectID=easInfo.getProjectNumber().getId().toString();
        	 DefineProjectInfo projectInfo= DefineProjectFactory.getLocalInstance(ctx).getDefineProjectInfo(new ObjectUuidPK(projectID));
        	 projectInfo.setYfzgAmount(UIRuleUtil.getBigDecimal(projectInfo.getYfzgAmount()).subtract(UIRuleUtil.getBigDecimal(easInfo.getEstimateAmount())));
        	 projectInfo.setNotkpzgAmount(UIRuleUtil.getBigDecimal(projectInfo.getNotkpzgAmount()).subtract(UIRuleUtil.getBigDecimal(easInfo.getEstimateAmount())));
        	 DefineProjectFactory.getLocalInstance(ctx).update(new ObjectUuidPK(projectID), projectInfo);
         }
//         if(easInfo.getSourceBillId()!=null){
//        	 CostBalanceInfo costBanInfo= CostBalanceFactory.getLocalInstance(ctx).getCostBalanceInfo(new ObjectUuidPK(easInfo.getSourceBillId())) ;
//             costBanInfo.setYfzgFinish(false);
//             costBanInfo.setZgyuAmount(easInfo.getEstimateAmount());
//             CostBalanceFactory.getLocalInstance(ctx).update(new ObjectUuidPK(easInfo.getSourceBillId()), costBanInfo);
//         }
    }
    protected void _unAudit(Context ctx, IObjectPK[] pks)throws BOSException, EASBizException
    {
         super._unAudit(ctx, pks);
         for(int i=0;i<pks.length;i++){
        	 EstimatedFinBillInfo easInfo =  (EstimatedFinBillInfo) this.getValue(ctx, pks[i]);
        	 if(easInfo.getProjectNumber()!=null){
            	 String projectID=easInfo.getProjectNumber().getId().toString();
            	 DefineProjectInfo projectInfo= DefineProjectFactory.getLocalInstance(ctx).getDefineProjectInfo(new ObjectUuidPK(projectID));
            	 projectInfo.setYfzgAmount(UIRuleUtil.getBigDecimal(projectInfo.getYfzgAmount()).subtract(UIRuleUtil.getBigDecimal(easInfo.getEstimateAmount())));
            	 projectInfo.setNotkpzgAmount(UIRuleUtil.getBigDecimal(projectInfo.getNotkpzgAmount()).subtract(UIRuleUtil.getBigDecimal(easInfo.getEstimateAmount())));
            	 DefineProjectFactory.getLocalInstance(ctx).update(new ObjectUuidPK(projectID), projectInfo);
             }
//        	 if(easInfo.getSourceBillId()!=null){
//            	 CostBalanceInfo costBanInfo= CostBalanceFactory.getLocalInstance(ctx).getCostBalanceInfo(new ObjectUuidPK(easInfo.getSourceBillId())) ;
//                 costBanInfo.setYfzgFinish(false);
//                 costBanInfo.setZgyuAmount(easInfo.getEstimateAmount());
//                 CostBalanceFactory.getLocalInstance(ctx).update(new ObjectUuidPK(easInfo.getSourceBillId()), costBanInfo);
//             }
      	}
    }
    
    protected void _delete(Context ctx, IObjectPK pk) throws BOSException,
    		EASBizException {
//    	EstimatedFinBillInfo estimafinbillinfo = EstimatedFinBillFactory.getLocalInstance(ctx).getEstimatedFinBillInfo(new ObjectUuidPK(pk.toString()));
//    	String sourbillid = estimafinbillinfo.getSourceBillId();
    	super._delete(ctx, pk);
//    	if(sourbillid!=null){
//    		DbUtil.execute(ctx, "/*dialect*/ update T_PM_CostBalance  set FYfzgFinish='0' where fid ='"+sourbillid+"'");
//    	}
    }
    protected IObjectPK _save(Context ctx, IObjectValue model)
    		throws BOSException, EASBizException {
//    	if(model.get("sourcebillid")!=null){
//    		DbUtil.execute(ctx, "/*dialect*/ update T_PM_CostBalance  set FYfzgFinish='1' where fid ='"+model.get("sourcebillid").toString()+"'");
//    	}
    	return super._save(ctx, model);
    	
    }
}